package org.phpgwt.tester.client.tests;

import java.util.Date;

import org.phpgwt.tester.client.remote.DateTestService;
import org.phpgwt.tester.client.remote.DateTestServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class DateTest extends PhpGwtTest {
	private static DateTestServiceAsync service = GWT.create(DateTestService.class);
	private static NumberFormat digit2 = NumberFormat.getFormat("00");
	private static NumberFormat digit3 = NumberFormat.getFormat("000");
	private static NumberFormat digit4 = NumberFormat.getFormat("0000");
	private static DateTimeFormat date = DateTimeFormat.getFormat("yyyyMMddHHmmssSSS");
	private static DateTimeFormat monthFormatter = DateTimeFormat.getFormat("MM");
	private static DateTimeFormat dayFormatter = DateTimeFormat.getFormat("dd");

	public static void run() {
		RootPanel.get().add(new Label("Date/time tests are for GMT+2 timezone. Failures may occure if your timezone is different."));

		service.getDate1(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate1", result, getDate(2012, 7, 22, 13, 8, 12, 303));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate1", caught);
			}
		});

		service.getDate2(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate2", result, getDate(2007, currentMonth(), currentDay()));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate2", caught);
			}
		});

		service.getDate3(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate3", result, getDate(2006, 7, currentDay()));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate3", caught);
			}
		});

		service.getDate4(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate4", result, getDate(2006, 7, 8));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate4", caught);
			}
		});

		service.getDate5(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate5", result, getDate(2006, 7, 8, 9, 10, 0, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate5", caught);
			}
		});

		service.getDate6(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate6", result, getDate(2006, 7, 8, 9, 10, 11, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate6", caught);
			}
		});

		service.getDate7(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate7", result, getDate(2006, 7, 8, 9, 10, 11, 123));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate7", caught);
			}
		});

		service.getDate8(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate8", result, getDate(2012, 11, 10, 9, 8, 0, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate8", caught);
			}
		});

		service.getDate9(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate9", result, getDate(2012, 11, 10, 9, 8, 7, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate9", caught);
			}
		});

		service.getDate10(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate10", result, getDate(2012, 11, 10, 9, 8, 7, 6));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate10", caught);
			}
		});

		service.getDate11(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate11", result, getDate(2012, 7, 20, 13, 14, 15, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate11", caught);
			}
		});

		service.getDate12(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate12", result, getDate(2012, 7, 20, 13, 14, 15, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate12", caught);
			}
		});

		service.getDate13(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate13", result, getDate(2012, 7, 20, 13, 14, 15, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate13", caught);
			}
		});

		service.getDate14(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate14", result, getDate(2012, 7, 20, 13, 14, 15, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate14", caught);
			}
		});

		service.getDate15(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate15", result, getDate(2012, 7, 22, 12, 11, 49, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate15", caught);
			}
		});

		service.getDate16(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate16", result, getDate(2012, 12, 20, 0, 0, 0, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate16", caught);
			}
		});

		service.getDate17(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate17", result, getDate(2012, 12, 20, 21, 22, 0, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate17", caught);
			}
		});

		service.getDate18(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate18", result, getDate(2012, 11, 10, 0, 0, 0, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate18", caught);
			}
		});

		service.getDate19(new AsyncCallback<Date>() {
			public void onSuccess(Date result) {
				printResult("getDate19", result, getDate(2012, 12, 29, 0, 0, 0, 0));
			}

			public void onFailure(Throwable caught) {
				printResult("getDate19", caught);
			}
		});

		service.sendDate1(getDate(1987, 6, 5, 4, 3, 2, 10), new AsyncCallback<Boolean>() {
			public void onSuccess(Boolean result) {
				printResult("sendDate1", result, true);
			}
			
			public void onFailure(Throwable caught) {
				printResult("sendDate1", caught);
			}
		});
		
		service.sendDate2(getDate(1987, 6, 5, 4, 3, 2, 10), new AsyncCallback<Boolean>() {
			public void onSuccess(Boolean result) {
				printResult("sendDate2", result, true);
			}
			
			public void onFailure(Throwable caught) {
				printResult("sendDate2", caught);
			}
		});
	}

	public static Date getDate(int year, int month, int day) {
		return getDate(year, month, day, 0, 0, 0, 0);
	}

	public static Date getDate(int year, int month, int day, int hour, int minute, int second, int ms) {
		return date.parse(digit4.format(year) + digit2.format(month == 0 ? 1 : month) + digit2.format(day == 0 ? 1 : day)
				+ digit2.format(hour) + digit2.format(minute) + digit2.format(second) + digit3.format(ms));
	}

	private static int currentMonth() {
		return Integer.parseInt(monthFormatter.format(new Date()));
	}

	private static int currentDay() {
		return Integer.parseInt(dayFormatter.format(new Date()));
	}
}
